﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
using System.Collections;
using System.Web.UI.WebControls;
using blackhouse.Web.Controls;
using blackhouse.Web.FasterFiles;
using System.ComponentModel;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Web.UI {

	[ParseChildren(true, "Items")]
	[Browsable(true)]
	public sealed class AinoGallery : Control {

		#region Fields

		private Panel pnlHolder;
		private string theme = "AinoGallery";

		#endregion

		#region Constructors

		public AinoGallery() {
			this.Items = new ArrayList();
		}

		#endregion

		#region Properties

		public ArrayList Items { get; set; }

		public string Theme { get { return this.theme; } set { this.theme = value; } }

		#endregion

		#region Event Handlers

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			JQueryLibraries jql = new JQueryLibraries();
			jql.IncludeFileBasedOnJQuery("blackhouse.Web.UI.AinoGallery.galleria.js,blackhouse.Web");
		}

		protected override void OnPreRender(EventArgs e) {
			base.OnPreRender(e);

			this.addStartScript();
		}

		#endregion

		#region Methods

		private void addStartScript() {
			string script = "<script type=\"text/javascript\">" + string.Format(@"
$(document).ready(function(){1}
	Galleria.loadTheme('{3}/galleria.js');
	$('#{0}').galleria();
{2});

</script> ", this.pnlHolder.ClientID, "{", "}", this.Theme);
			if(!this.Page.ClientScript.IsClientScriptBlockRegistered("agallery"))
				this.Page.ClientScript.RegisterClientScriptBlock(typeof(AinoGallery), "agallery", script, false);
		}

		protected override void CreateChildControls() {
			pnlHolder = new Panel();
			this.Controls.Add(pnlHolder);
			foreach(AinoGalleryItem item in this.Items) {
				if(item == null) continue;

				HyperLink link = new HyperLink();
				pnlHolder.Controls.Add(link);
				link.NavigateUrl = item.FullImageUrl;
				link.ImageUrl = item.ThumbailImageUrl;
				link.ToolTip = item.Title;
			}
		}

		#endregion

	}
}